//
//  FansTrendViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/2/10.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "FansTrendViewController.h"
#import "FansTrendDetailViewController.h"
#import "FanstrdNormalCell.h"
#import "TrendBottomView.h"
#import "QZHSearchBar.h"
#import <WebKit/WebKit.h>
#import "FansTrdFrameModel.h"
#import "FansTrdModel.h"

@interface FansTrendViewController ()<UITableViewDelegate,UITableViewDataSource,FanstrdNormalCellDelegate,UITextFieldDelegate>{
    NSMutableArray *_dataArray;
    NSMutableArray *_dataFrameArray;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong)  WKWebView *webView;
@property (nonatomic, assign) int page;
@property (nonatomic, assign) int maxPage;
@end

@implementation FansTrendViewController

#pragma mark --  动态列表发送请求
- (void)FTrendListRequest {
    NSDictionary *dict = @{
                           @"udid":User_ID,
                           @"token":TOKEN,
                           @"page":[NSNumber numberWithInt:self.page]
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kFTrendList];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        [self endRefresh];
        if ([responseObject[@"code"] intValue] == 200) {
            if (self.page == 1 && _dataFrameArray.count > 0) {
                [_dataFrameArray removeAllObjects];
                [_dataArray removeAllObjects];
            }
            self.maxPage = ceilf([responseObject[@"data"][@"num"] floatValue]/20);
            NSArray *array = responseObject[@"data"][@"list"];
            for (NSDictionary *trendDict in array) {
                FansTrdModel *model = [[FansTrdModel alloc] initWithDict:trendDict];
                NSArray *imageArray = [model.c_images componentsSeparatedByString:@","];
                model.pictureArray = [NSMutableArray arrayWithArray:imageArray];
                [_dataArray addObject:model];
                FansTrdFrameModel *frameModel = [[FansTrdFrameModel alloc] init];
                frameModel.trendModel = model;
                [_dataFrameArray addObject:frameModel];
            }
            [self.tableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        [self endRefresh];
    } showHUD:NO];
}


//动态点赞
- (void)praiseClickRequestWithCid:(NSString *)cid IndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           @"cid":cid,
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kTrendLike];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            FansTrdFrameModel *frameModel = _dataFrameArray[indexPath.row];
            NSString *status = responseObject[@"data"][@"status"];
            frameModel.trendModel.booked = status;
            if ([status intValue] == 10) {//点赞成功
                frameModel.trendModel.c_like = [NSString stringWithFormat:@"%d",[frameModel.trendModel.c_like intValue] + 1];
            } else {//取消点赞
                frameModel.trendModel.c_like = [NSString stringWithFormat:@"%d",[frameModel.trendModel.c_like intValue] - 1];
            }
            [self.tableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    [self FTrendListRequest];
    _dataArray = [NSMutableArray arrayWithCapacity:1];
    _dataFrameArray = [NSMutableArray arrayWithCapacity:1];
    [self setUpUI];
    // Do any additional setup after loading the view.
}


#pragma mark -- UI
- (void)setUpUI {
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpNav];
    [self creatTableView];
}

/**
 *  停止刷新
 */
-(void)endRefresh{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}
#pragma mark -- UI

- (void)setUpNav {
    self.navigationItem.title = @"星友圈";
}
#pragma mark --  消息中心
- (void)TrendCenterClick:(UIButton *)sender {
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT - kNAV_HEIGHT - kTABBAE_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableHeaderView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, 55);
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self FTrendListRequest];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        if (self.page == self.maxPage+1) {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        } else {
            [self FTrendListRequest];
        }
    }];
    [self.view addSubview:self.tableView];
}
#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataFrameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FanstrdNormalCell *cell = [FanstrdNormalCell loadNormalCellWithTabelView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    FansTrdFrameModel *model = _dataFrameArray[indexPath.row];
    cell.frameModel = model;
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FansTrendDetailViewController *detailVC = [FansTrendDetailViewController new];
    FansTrdFrameModel *model = _dataFrameArray[indexPath.row];
    detailVC.trendFrameModel = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark -- 区头视图

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    FansTrdFrameModel *model = _dataFrameArray[indexPath.row];
    return model.cellHeight;
}

#pragma mark -- 点赞
- (void)praiseClickwithCell:(FanstrdNormalCell *)trendcell Tag:(int)actionTag {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:trendcell];
    FansTrdFrameModel *frameModel = _dataFrameArray[indexPath.row];
    switch (actionTag) {
        case 0:{//点赞
            [self praiseClickRequestWithCid:frameModel.trendModel.cid IndexPath:indexPath];
        }
            break;
        case 1:{//评论
            FansTrendDetailViewController *detailVC = [FansTrendDetailViewController new];
            FansTrdFrameModel *model = _dataFrameArray[indexPath.row];
            detailVC.trendFrameModel = model;
            [self.navigationController pushViewController:detailVC animated:YES];
        }
            break;
        case 2:{//分享
            FansTrendDetailViewController *detailVC = [FansTrendDetailViewController new];
            FansTrdFrameModel *model = _dataFrameArray[indexPath.row];
            detailVC.trendFrameModel = model;
            [self.navigationController pushViewController:detailVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
